<template>
  <VPage>
    <VTable ref="tableRef" v-bind="tableOptions">
      <template #form="{ form }">
        <VGroup>
          <el-select v-model="form.input.type" style="width: 110px">
            <el-option label="报损单号" value="damage_no"></el-option>
            <el-option label="异常单号" value="abnormal_no"></el-option>
            <el-option label="采购单号" value="purchase_no"></el-option>
            <el-option label="样品单号" value="sample_no"></el-option>
          </el-select>
          <VBatchInput v-model="form.input.value" @search="query" class="w-60" />
        </VGroup>
        <VGroup>
          <el-select v-model="form.skuInfo.type" class="w-28">
            <el-option label="sku" value="sku"></el-option>
            <el-option label="赛盒SKU" value="sai_he_sku"></el-option>
            <el-option label="自定义sku" value="custom_sku"></el-option>
          </el-select>
          <VBatchInput v-model="form.skuInfo.value" @search="query" class="w-60" />
        </VGroup>
        <VSelect v-model="form.status" placeholder="状态" :options="options.status" multiple collapse-tags filterable clearable style="min-width: 160px" />
        <VSelect v-model="form.type" placeholder="来源类型" :options="options.source_type" multiple collapse-tags filterable clearable style="min-width: 160px" />
        <VSelect v-model="form.supplier_name" placeholder="供应商" type="supplierName" allow-create multiple collapse-tags filterable clearable style="min-width: 160px" />
        <!-- <VSelect v-model="form.supplier_name" placeholder="供应商" type="supplierName" allow-create style="min-width: 200px" multiple
          collapse-tags clearable filterable /> -->
        <VSelect v-model="form.warehouse_id" placeholder="报废仓库" type="warehouse156" multiple collapse-tags filterable clearable style="min-width: 160px" />
        <VCascader v-model="form.create_id" placeholder="申请人" multiple collapse-tags filterable clearable style="min-width: 160px" />
        <VCascader v-model="form.audit_id" placeholder="审批人" multiple collapse-tags filterable clearable style="min-width: 160px" />
        <VCascader v-model="form.warehouse_id" placeholder="仓库处理人" multiple collapse-tags filterable clearable style="min-width: 160px" />
        <VGroup>
          <el-select v-model="form.dateRange.type" style="width: 130px">
            <el-option label="单据生成时间" value="create_time"></el-option>
            <el-option label="仓库处理时间" value="warehouse_time"></el-option>
            <el-option label="审核时间" value="audit_time"></el-option>
          </el-select>
          <VDate v-model="form.dateRange.value" />
        </VGroup>
      </template>
      <template #toolbar_btns>
        <VAudit auth="/purchase/manage/damage/financeConfirm" field="d_id" url="/v1/damage/damage/verify" :tableRef="tableRef" :list="[{ label: '待财务确认/待CEO审核', value: 1 }]"
          @success="query" />
        <VExport auth="/purchase/manage/damage/export" :name="baseName" :tableRef="tableRef" mainKey="d_id" task class="ml-auto" />
      </template>
    </VTable>
    <Add ref="addRef" :options="options" @success="query" />
    <VLog ref="logRef" />
  </VPage>
</template>
  
<script setup lang="jsx" name="Account">
import api from './api'
import Add from './components/Add'
import { getOption, warehouse } from '@/api/public'
import { ElMessage, ElMessageBox } from 'element-plus'

const baseName = 'damage'
const tableRef = ref()
const logRef = ref()
const loadings = reactive({})
// 查询
const query = () => {
  tableRef?.value.query()
}

// 添加
const addRef = ref()

// 获取options
const options = ref({})
Promise.all([
  getOption({ type: baseName }).then(res => res.data),
  getOption({ type: 'purchase' }).then(res => res.data),
  Promise.resolve({ test: [{ label: '没有数据', value: 1 }] })
]).then(res => {
  options.value = res.reduce((acc, cur) => (((acc = { ...acc, ...cur }), acc)), {})
})

// 单行操作
const handleRow = (name, row, rowIndex) => {
  const { d_id, supplier_id } = row
  const showLoading = () => {
    loadings[name + rowIndex] = true
  }
  const closeLoading = () => {
    loadings[name + rowIndex] = false
  }
  const handle = {
    look: () => {
      addRef?.value.open(row, true)
    },
    edit: () => {
      addRef?.value.open(row)
    },
    submit: () => {
      showLoading()
      api.submit({ d_id }).then(res => {
        ElMessage.success('操作成功')
        query()
      }).finally(closeLoading)
    },
    cancel: () => {
      ElMessageBox.confirm('是否确认作废？', '取消', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      })
        .then(() => {
          showLoading()
          api.cancel({ d_id }).then(res => {
            ElMessage.success('操作成功')
            query()
          }).finally(closeLoading)
        })
        .catch(() => {
          ElMessage({ type: 'info', message: '取消操作', })
        })
    },
    audit: () => {
      auditRef?.value.open([row], { label: '审核', value: 2 })
    },
    log: () => {
      logRef?.value.open({ log_type: baseName, d_id })
    },
  }
  handle[name]()
}

// 列表配置
const tableOptions = reactive({
  rowConfig: { height: 72 },
  showHeaderOverflow: false,
  formConfig: {
    data: {
      input: { type: 'damage_no', value: '' },
      skuInfo: { type: 'sku', value: '' },
      dateRange: { type: 'create_time', value: [], range: true },
    }
  },
  columns: [
    { type: 'checkbox', width: 40, fixed: 'left' },
    {
      width: 150, slots: {
        header: () => <div>报损单号<br/>采购单号</div>,
        default: ({ row }) => <div>
        <VText value={row.damage_no} type='link' onClick={() => handleRow('look', row)} />
        <VText value={row.purchase_no} />
        </div>
      }
    },
    {
      width: 180, slots: {
        header: () => <div>供应商信息</div>,
        default: ({ row }) => <div>
          <VText title='供应商' value={row.supplier_name} />
          <VText title='供应商id' value={row.supplier_code} />
          <VText title='结算方式' value={row.period_name} />
        </div>
      }
    },
    {
      width: 120,title: '来源类型', field: 'source_type_name'
    },
    {
      width: 140, slots: {
        header: () => <div>来源单号</div>,
        default: ({ row }) => <VText value={row.source_no} />
      }
    },
    {
      width: 100, slots: {
        header: () => <div>报损单状态</div>,
        default: ({ row }) => row.status_name
      }
    },
    {
      width: 100,title: 'sku', field: 'sku'
    },
    {
      title: '图片', width: 80, slots: {
        default: ({ row }) => <VImage src={row.main_img} size="60px"></VImage>
      }
    },
    {
      width: 140, title: '报损仓库', field: 'warehouse_id_name'
    },
    {
      width: 100,title: '报损数量', field: 'count'
    },
    {
      width: 100,title: '报损总额', field: 'total_money'
    },
    {
      width: 120, slots: {
        header: () => <div>报损备注</div>,
        default: ({ row }) => <VText value={row.remark} line={3} />
      }
    },
    {
      width: 170, slots: {
        header: () => <div>申请人<br />生成时间</div>,
        default: ({ row }) => <div>{row.create_user}<br />{row.create_time}</div>
      }
    },
    {
      width: 170, slots: {
        header: () => <div>审核人<br />审核时间</div>,
        default: ({ row }) => <div>{row.audit_user}<br />{row.audit_time}</div>
      }
    },
    {
      width: 120, slots: {
        header: () => <div>审核备注</div>,
        default: ({ row }) => <VText value={row.audit_reason} line={3} />
      }
    },
    {
      title: '操作', width: 110, fixed: 'right', slots: {
        default: ({ row, rowIndex }) => {
          const { status } = row
          // status	报损单状态：1、待提，,2、待审核，3、待财务确认，4、待CEO审核，5、已驳回，6、已完成，7、已作废
          return [<div class='nm flex flex-wrap gap-x-2 gap-y-1'>
            <VButton auth="/purchase/manage/damage/detail" link type="primary" onClick={() => handleRow('look', row, rowIndex)}>查看</VButton>
            {[1, 5].includes(status) ? <VButton auth="/purchase/manage/damage/edit" link type="primary" onClick={() => handleRow('edit', row, rowIndex)}>编辑</VButton> : ''}
            {[1, 5].includes(status) ? <VButton auth="/purchase/manage/damage/submit" link type="primary" onClick={() => handleRow('submit', row, rowIndex)}>提交</VButton> : ''}
            {[1, 5].includes(status) ? <VButton auth="/purchase/manage/damage/cancel" link type="primary" onClick={() => handleRow('cancel', row, rowIndex)}>作废</VButton> : ''}
            <VButton auth="/purchase/manage/damage/log" link type="primary" onClick={() => handleRow('log', row, rowIndex)}>日志</VButton>
          </div>]
        }
      }
    },
  ],
  proxyConfig: {
    autoLoad: false,
    ajax: {
      query: ({ page, form }) => api.query({ ...form, ...page })
    }
  }
})
</script>
  